import {observable , action, runInAction} from 'mobx'
import axios from 'axios'
import { message } from 'antd'
import {apiUrl} from '../api/constant'
import {fetchData} from '../api/datasource'

const urlConfig = {
    getCategoryList: '/category/getCategoryList',
    insertCategory: '/category/insertCategory',
    updateCategory: '/category/updateCategory',
    updateCategorySort: '/category/updateCategoryList',
}

class itemStore {
    @observable loading = false
    @observable categoryList = []
    @observable buttonLoading = false

    @action('获取类别清单') getCategoryList = async (params) => {
        this.loading = true
        const data = encodeURI(JSON.stringify(params))
        const result = await fetchData(urlConfig.getCategoryList, 'get', data)
        if (result.status !== 200) {
            message.error('数据加载失败')
            this.categoryList = []
        }
        runInAction(() => {
            this.loading = false
            if (result.data.data) {
                this.categoryList = result.data.data
            } else {
                this.categoryList = []
            }
        })
    }

    @action('增加类别') insertCategory = async (params) => {
        this.buttonLoading = true
        const data = encodeURI(JSON.stringify(params))
        const result = await fetchData(urlConfig.insertCategory, 'post', data)
        if (result.status !== 200) {
            message.error('操作失败')
        }
        runInAction(() => {
            this.buttonLoading = false

        })
        return result.data
    }

    @action('修改类别') updateCategory = async (params) => {
        this.buttonLoading = true
        const data = encodeURI(JSON.stringify(params))
        const result = await fetchData(urlConfig.updateCategory, 'post', data)
        if (result.status !== 200) {
            message.error('操作失败')
        }
        runInAction(() => {
            this.buttonLoading = false
        })
        return result.data
    }

    @action('修改类别排序') updateCategorySort = async (params) => {
        this.buttonLoading = true
        const data = encodeURI(JSON.stringify(params))
        const result = await fetchData(urlConfig.updateCategorySort, 'post', data)
        if (result.status !== 200) {
            message.error('操作失败')
        }
        runInAction(() => {
            this.buttonLoading = false
        })
        return result.data
    }
}

export default new itemStore()